package yui.bss.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import yui.comn.hub.extension.config.HubExtensionProperties;
import yui.comn.hub.extension.controller.Controller;
import yui.comn.hub.xml.parser.HubXmlCache;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 控制器的基类，将公共方法写在这个类中
 *
 * @author yi.yuy
 */

public abstract class BaseController extends Controller {

    @Autowired
    private HubExtensionProperties properties;

    protected final Logger log = LoggerFactory.getLogger(getClass());


    /********************************************消息解析*****************************************************/
    protected Object buildResult(int code, Object data, IPage<?> page, String msg, String gridName) {
        Map<String, Object> root = new LinkedHashMap<>();
        root.put("code", code);
        if (null != msg) {
            root.put("msg", msg);
        }

        if (null != gridName && this.properties.isOpenHeader()) {
            root.put("header", HubXmlCache.getInstance().toGridKvMap(this.getClass(), gridName));
        }

        if (null != data && null == page) {
            root.put("data", data);

        }

        if (null != page) {
            Map<String, Object> resultData = new HashMap<>();
            if (null != data) {
                resultData.putAll((Map) data);
            }
            Map<String, Object> pagination = new HashMap();
            pagination.put("total", page.getTotal());
            pagination.put("pageSize", page.getSize());
            pagination.put("pageNum", page.getCurrent());
            resultData.put("page", pagination);
            root.put("data", resultData);
        }

        return root;
    }

}
